﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace JuridicoDataLibrary.Helper
{
    public class HibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        private static ISessionFactory SessionFactory(bool createDatabase)
        {
            if ((_sessionFactory != null) && (!createDatabase))
                return _sessionFactory;

            var config = new Configuration().Configure();
            config.AddAssembly("JuridicoDataLibrary");

            if (createDatabase)
            {
                var schemaExport = new SchemaExport(config);
                schemaExport.Drop(false, true);
                schemaExport.Create(false, true);
            }

            _sessionFactory = config.BuildSessionFactory();
            return _sessionFactory;
        }

        public static ISession OpenSession()
        {
            return SessionFactory(false).OpenSession();
        }

        public static void CriaDatabase()
        {
            SessionFactory(true);
        }
    }
}
